window.onload=function () {

    searchBar();

    banner();

    timeCount();

};

//1导航搜索栏
function searchBar(){
    var searchBack=document.querySelector(".jd_search_box");
    window.addEventListener('scroll',function () {
        var height=document.querySelector(".jd_banner").offsetHeight;
        // var scrollH = document.documentElement.scrollTop;
        // var scrollH=document.body.scrollTop;
        var scrollTop= window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
        var opacity=0;
        if(height>scrollTop){
            opacity=scrollTop/height*0.85
        }else{
            opacity=0.85
        }
        searchBack.style.background="rgba(201,21,35,"+opacity+")";

    })
}
//2轮播图
function banner(){
//      轮播图无缝
//         点随图走
//     自动播放
//     手势操作停止轮播
//     上下图拨动
//     手势滑动距离不足  吸附效果
    var bannerBox=document.getElementById("bannerBox");
    var pointBox=document.querySelector("#pointBox");
    var points=pointBox.querySelectorAll("li");
    var imageBox=bannerBox.querySelector("li");
    var index=1;

    //函数
    function setTransition() {
        bannerBox.style.transition="all 0.5s";
        bannerBox.style.webkitTransition="all 0.5s";
    }
    function transformX(shifting) {
        bannerBox.style.transform="translateX("+shifting+"px)";
        bannerBox.style.webkitTransform="translateX("+shifting+"px)";
    }
    function clearTransition() {
        bannerBox.style.transition="none";
        bannerBox.style.webkitTransition="none";
    }


    var timeMachine=setInterval(function timeMachine() {
        index++;
        var imgWidth=imageBox.offsetWidth;
        setTransition();
        transformX(-index*imgWidth);
    },2000);

    bannerBox.addEventListener("transitionend",function(){
        var imgWidth=imageBox.offsetWidth;
        if(index>=9){
            index=1;
            clearTransition();
            transformX(-index*imgWidth)

        }else if(index<=0){
            index=8;
            clearTransition();
            transformX(-index*imgWidth)
        }
        //点随图走
        setPoints();
    });
    //点随图走函数
    function setPoints(){
        points.forEach(function(item,dd){
            item.classList.remove("now");
            points[index-1].classList.add("now");
        })
    }

    var bindSwipeEvent=function(dom,rightSwipe,leftSwipe){
        var startX=0;
        var distance=0;
        var isMove=false;
        dom.addEventListener("touchstart",function (e) {
            startX=e.touches[0].clientX;
            clearInterval(timeMachine);
        });
        dom.addEventListener("touchmove",function (e) {
            var imgWidth=imageBox.offsetWidth;
            var moveX=e.touches[0].clientX;
            distance=moveX-startX;
            var translateX=-index*imgWidth+distance;
            isMove=true;
            clearTransition();
            transformX(translateX);
        });
        dom.addEventListener("touchend",function (e) {
            var swipeSize=imageBox.offsetWidth/3;
            var imgWidth=imageBox.offsetWidth;
            setTransition();
            // 如果滑动超过1/3
            if (isMove && Math.abs(distance)>swipeSize) {
                // >0 向右划动
                if(distance>0){
                    index--;
                    rightSwipe && rightSwipe.call(dom,e);
                }else{//<0向左划动
                    index++;
                    leftSwipe && leftSwipe.call(dom,e);
                }
                transformX(-index*imgWidth);
            }else{
                transformX(-index*imgWidth);
            }
            //重置参数和定时器
            clearInterval(timeMachine);
            startX=0;
            distance=0;
            isMove=false;
            timeMachine=setInterval(function() {
                index++;
                var imgWidth=imageBox.offsetWidth;
                setTransition();
                transformX(-index*imgWidth);
            },2000);
        });
    };

    bindSwipeEvent(bannerBox,function(e) {

    },function(e) {

    })

}

//3倒计时
function timeCount() {
    var timeList=document.querySelector(".time").children;
    var countTime=2*60*60;
    var tm=setInterval(function () {
        countTime--;
        var hours=Math.floor(countTime/(60*60));
        var minuts=Math.floor(countTime%(60*60)/60);
        var seconds=countTime%60;
        timeMove(hours,minuts,seconds);
        countTime<0?clearInterval(tm):"";
    },1000);
    function timeMove(hours,minuts,seconds) {
        //小时设置
        if (hours>=10){
            timeList[0].innerHTML=Math.floor(hours/10);
            timeList[1].innerHTML=hours%10;
        }else{
            timeList[1].innerHTML=hours;
        }
        //分时设置
        if (minuts>=10){
            timeList[3].innerHTML=Math.floor(minuts/10);
            timeList[4].innerHTML=minuts%10;
        }else{
            timeList[4].innerHTML=minuts;
        }
        //秒时设置
        if (seconds>=10){
            timeList[6].innerHTML=Math.floor(seconds/10);
            timeList[7].innerHTML=seconds%10;
        }else{
            timeList[7].innerHTML=seconds;
        }
    }

    // console.log(timeBox.querySelector("li:nth-child(4)"));

}
